package com.gopay.common.user.manager.impl;

import com.alibaba.fastjson.JSONArray;
import com.gopay.common.domain.user.PwdCtrlAttrInfo;
import com.gopay.common.user.dao.PwdCtrlAttrInfoDao;
import com.gopay.common.user.manager.PwdCtrlAttrInfoManager;
import com.gopay.common.util.FastJsonUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author xuexj
 * @Date 2017/1/6 10:47
 * @Desc 密码控件信息采集入库
 */
@Component(value = "pwdCtrlAttrInfoManager")
public class PwdCtrlAttrInfoManagerImpl implements PwdCtrlAttrInfoManager {
    protected final static Log logger = LogFactory.getLog(PwdCtrlAttrInfoManagerImpl.class);
    @Autowired
    public PwdCtrlAttrInfoDao pwdCtrlAttrInfoDao;

    @Override
    public PwdCtrlAttrInfo findById(Integer id){
        return pwdCtrlAttrInfoDao.find(PwdCtrlAttrInfo.class, id);
    }

    /**
     * @param info 【封装operSource、userId、custId、gopayOrderId等信息】
     * @return void
     * @Desc 保存安全控件附属信息
     * {"mac":["","00-FF-98-96-46-F8","5C-C5-D4-F8-FD-21","68-F7-28-4F-F1-66","5C-C5-D4-F8-FD-1D"],"ip_address":["192.168.100.10","0.0.0.0","0.0.0.0","0.0.0.0","192.168.31.136"],"hd_sn":["RC055ACB2N76YJ"],"cpu_sn":[""],"bios_sn":"","os_name_version":"Windows 7 Ultimate (6.1.7601)","explore_name_version":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0","gmt_time":"20170108215603","time_zone":"China Standard Time","default_language":"zh-CN","screen_resolution":["1920*1080"]}
     */
    @Override
    public Integer savePwdCtrlAttrInfo(PwdCtrlAttrInfo info) {
        String jsonStr = info.getRemark();
        if (StringUtils.isEmpty(jsonStr)){
            logger.error("密码控件未获取到 hardList！");
            info.setOsInfo("MAC");
        }else{
            Map<String,Object> map = FastJsonUtil.stringToCollect(jsonStr);
            Map<String,String> ret = new HashMap<>();

            for (String key : map.keySet()){
                StringBuffer sbf = new StringBuffer();
                Object o = map.get(key);
                if (o != null){
                    if (o instanceof JSONArray) {
                        JSONArray obj = (JSONArray) o;
                        for (int i = 0; i < obj.size(); i++){
                            sbf.append(obj.get(i));
                            if (i != obj.size() - 1){
                                sbf.append(",");
                            }
                        }
                    }else {
                        sbf.append(o);
                    }
                }
                ret.put(key,sbf.toString());
            }
            info.setMac(ret.get("mac"));
            info.setIp(ret.get("ip_address"));
            if (StringUtils.isBlank(info.getCpuSn())){
                info.setCpuSn(ret.get("cpu_sn"));
            }
            if (StringUtils.isBlank(info.getHdSn())){
                info.setHdSn(ret.get("hd_sn"));
            }
            info.setBiosSn(ret.get("bios_sn"));
            info.setOsInfo(ret.get("os_name_version"));
            info.setExplorerInfo(ret.get("explore_name_version"));
            info.setOperTime(ret.get("gmt_time"));
            info.setTmZone(ret.get("time_zone"));
            info.setDefLang(ret.get("default_language"));
            info.setScrRsl(ret.get("screen_resolution"));
        }
        PwdCtrlAttrInfo save;
        try {
            /*修改network超长的问题 added by wangdong at 2017-07-28 start*/
            if (StringUtils.isNotEmpty(info.getNetwork()) && info.getNetwork().length()>700){
                info.setNetwork(info.getNetwork().substring(0,700));
            }
            /*修改network超长的问题 added by wangdong at 2017-07-28 end*/
            save = pwdCtrlAttrInfoDao.save(info);
        }catch (Exception e){
            /*出现异常后，保存关键信息 added by wangdong at 2017-04-09 start*/
            PwdCtrlAttrInfo excepInfo = new PwdCtrlAttrInfo();
            excepInfo.setRealIp(info.getRealIp());
            excepInfo.setOperSource(info.getOperSource());
            excepInfo.setStatus(info.getStatus());
            excepInfo.setCustId(info.getCustId());
            excepInfo.setGopayOrderId(info.getGopayOrderId());
            excepInfo.setOperTime(info.getOperTime());
            excepInfo.setRemark(info.getRemark());
            excepInfo.setUserId(info.getUserId());
            excepInfo.setSuspicion(info.getSuspicion());
            excepInfo.setOsBrowser(info.getOsBrowser());
            save = pwdCtrlAttrInfoDao.save(excepInfo);
            /*出现异常后，保存关键信息 added by wangdong at 2017-04-09 end*/
        }
        return save.getId();
    }

}
